
#include "CardMem.h"
#include "CardPrivate.h"
#include "CardMemPrivate.h"

extern const CardFactory_T mifareCardFactory;
const CardFactory_T* memCardFactorys[Card_T_MemMax] = {
	[Card_T_Mifare] = &mifareCardFactory,
};

Card_T * Card_Mem_Create(WCI * wci, unsigned char type)
{
	const CardFactory_T* f = memCardFactorys[type];
	if (f) {
		return f->create(wci);
	}
	return (Card_T*)0;
}

void Card_Mem_Destroy(Card_T* card) {
	if (card) {
		const CardFactory_T* f = memCardFactorys[card->type];
		f->destroy(card);
	}
}

static const MemCardOps_T* Card_Mem_Ops(Card_T* card) {
	return card ? (const MemCardOps_T*)card->ops : (const MemCardOps_T*)0;
}

int Card_Mem_Setup(Card_T* card, void* param){
	const MemCardOps_T* op = Card_Mem_Ops(card);
	return op ? op->setup(card, param) : -1;
}

int Card_Mem_Authen(Card_T* card, void* param, uint8_t* key, uint8_t klen) {
	const MemCardOps_T* op = Card_Mem_Ops(card);
	return op ? op->authen(card, param, key, klen) : -1;
}

int Card_Mem_Read(Card_T* card, uint32_t address, uint8_t* buf, uint16_t len) {
	const MemCardOps_T* op = Card_Mem_Ops(card);
	return op ? op->read(card, address, buf, len) : -1;
}

int Card_Mem_Write(Card_T* card, uint32_t address, uint8_t* buf, uint16_t len) {
	const MemCardOps_T* op = Card_Mem_Ops(card);
	return op ? op->write(card, address, buf, len) : -1;
}

int Card_Mem_Control(Card_T* card, uint8_t opcode, void* param, void* cbuf, uint16_t clen, void* rbuf, uint16_t *rlen) {
	const MemCardOps_T* op = Card_Mem_Ops(card);
	return op ? op->control(card, opcode, param, cbuf, clen, rbuf, rlen) : -1;
}



